const http = require('http');

const child_process = require('child_process');

const server = http.createServer();

const cpulen = require('os').cpus().length;

server.listen(3000)

let workers = {};

function createProcess(){
    let worker = child_process.fork('./worker.js'); //

    console.log("打印创建之间的",worker.pid);

    worker.send('server',server);

    workers[worker.pid] = worker;
    

    //结束进程，重启
    worker.on('exit',() => {
        delete workers[worker.pid];
        createProcess()
    })

    //捕获异常重启
    worker.on('message',info => {
        console.log(info)
        // if(info.msg === 'error'){
        //     console.log("=====",info.error)
        // }
    })
}

for(let i = 0;i<cpulen;i++){
    createProcess()
}

process.on('exit',() => {
    for(let i in workers){
        workers[i].kill()
    }
})

